package com.gxw.rxjavatest.model;


import android.util.Log;

import com.gxw.rxjavatest.data.respone.GetInfoResponse;
import com.gxw.rxjavatest.manager.LoginParams;
import com.gxw.rxjavatest.model.bean.LoginID;
import com.gxw.rxjavatest.model.bean.User;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by GXW on 2016/4/7 0007.
 * email:603004002@qq.com
 */
public class UserModel {

    private String TAG = UserModel.class.getName().toString();

    public Observable<User> getUser() {

        return Observable.create(new Observable.OnSubscribe<User>() {
            @Override
            public void call(final Subscriber<? super User> subscriber) {
                // 设置个2000ms的延迟，模拟网络访问、数据库操作等等延时操作
//                SystemClock.sleep(2000);

//                RequestParams requestParams = new RequestParams();
//                requestParams.addBodyParameter("loginID", "gxw");
//                requestParams.addBodyParameter("password", Strings.md5("cnea2015"));
//                requestParams.addBodyParameter("deviceType", "android");

                LoginParams loginParams = new LoginParams("gxw", "cnea2015", "android");

                x.http().post(loginParams, new Callback.CacheCallback<List<GetInfoResponse>>() {
                    @Override
                    public boolean onCache(List<GetInfoResponse> loginIDs) {

                        Log.i(TAG, " onCache: " + loginIDs.get(0).getData().toString());
                        return false;
                    }

                    @Override
                    public void onSuccess(List<GetInfoResponse> loginIDs) {

                        Log.i(TAG, " onSuccess: " + loginIDs.get(0).getData().toString());
                        LoginID data = loginIDs.get(0).getData();
                        User user = new User(data.getName(), 1, 1);
                        subscriber.onNext(user);
                        subscriber.onCompleted();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i(TAG, " ---------------- onError ---------------- " + ex.getMessage().toString());

                        subscriber.onError(ex);
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        Log.i(TAG, " ---------------- onCancelled ---------------- " + cex.getMessage().toString());
                    }

                    @Override
                    public void onFinished() {

                    }
                });

//                Log.i("sequence","-------------- call --------------");
//
//                Retrofit retrofit = new Retrofit.Builder()
//                        .baseUrl("http://117.79.231.125:17080/wcf.svc")
//                        .addConverterFactory(GsonConverterFactory.create())
//                        .addCallAdapterFactory(GuavaCallAdapterFactory.create())
//                        .build();
//
//                ApiService apiService = retrofit.create(ApiService.class);
//                apiService.getInfo("117.79.231.125:17080")
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Subscriber<GetInfoResponse>() {
//                            @Override
//                            public void onCompleted() {
//
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//
//                            }
//
//                            @Override
//                            public void onNext(GetInfoResponse getInfoResponse) {
//
//                            }
//                        });

//                User user = new User("gxw", 23, 1);
//                if (user == null) {
//
//                } else {
//                    subscriber.onNext(user);
//                    subscriber.onCompleted();
//                }

            }
        });

    }

}
